﻿using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;

namespace Dynamic.Core.Utility
{
    /// <summary>
    /// 编码通用类
    /// </summary>
    public class EncodingUtility
    {

        /// <summary>
        /// 采用指定的编码方式对Url字符串进行解码
        /// </summary>
        /// <param name="rawUrl"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public string DeEncodeUrl(string rawUrl, Encoding e)
        {
            return HttpUtility.UrlDecode(rawUrl, e);
        }

        /// <summary>
        /// 采用gb2312解码Url 
        /// </summary>
        /// <param name="rawUrl"></param>
        /// <returns></returns>
        public string DeEncodeUrlWithGB2312(string rawUrl)
        {
            return this.DeEncodeUrl(rawUrl, Encoding.GetEncoding("gb2312"));
        }

        /// <summary>
        /// 采用指定的编码方式对Url字符串进行编码
        /// </summary>
        /// <param name="rawUrl"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public string EncodingUrl(string rawUrl, Encoding e)
        {
            return HttpUtility.UrlEncode(rawUrl, e);
        }

        /// <summary>
        /// 采用gb2312编码Url字符串
        /// </summary>
        /// <param name="rawUrl"></param>
        /// <returns></returns>
        public string EncodingUrlWithGB2312(string rawUrl)
        {
            return this.EncodingUrl(rawUrl, Encoding.GetEncoding("gb2312"));
        }

        //public static string GetBytesEncodingName(byte[] input)
        //{
        //    return IdentifyEncoding.GetEncodingString(input);
        //}

        //public static string GetFileEncodingName(string fullFileName)
        //{
        //    FileInfo fi = new FileInfo(fullFileName);
        //    return IdentifyEncoding.GetEncodingString(fi);
        //}

        public static Encoding GetGB2312Encoding()
        {
            return Encoding.GetEncoding("GB2312");
        }

        //public static string GetUrlEncodingName(string urlAddress)
        //{
        //    return IdentifyEncoding.GetEncodingString(new Uri(urlAddress));
        //}

        public static string QP2String(string qpString, string charset)
        {
            Encoding encoding = Encoding.GetEncoding(charset);
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < qpString.Length; i++)
            {
                if (qpString[i] == '=')
                {
                    char ch = qpString[i + 1];
                    ch = qpString[i + 2];
                    string str = ch.ToString() + ch.ToString();
                    ch = qpString[i + 4];
                    ch = qpString[i + 5];
                    string str2 = ch.ToString() + ch.ToString();
                    byte num2 = Convert.ToByte(str, 0x10);
                    byte num3 = Convert.ToByte(str2, 0x10);
                    builder.Append(encoding.GetString(new byte[] { num2, num3 }));
                    i += 5;
                }
                else
                {
                    builder.Append(qpString[i]);
                }
            }
            return builder.ToString();
        }

        public static string String2QP(string normalString, string charset)
        {
            Encoding encoding = Encoding.GetEncoding(charset);
            StringBuilder builder = new StringBuilder();
            foreach (char ch in normalString)
            {
                if (ch < '\x007f')
                {
                    builder.Append(ch);
                }
                else
                {
                    byte[] bytes = encoding.GetBytes(ch.ToString());
                    builder.Append("=" + Convert.ToString(bytes[0], 0x10).ToUpper() + "=" + Convert.ToString(bytes[1], 0x10).ToUpper());
                }
            }
            return builder.ToString();
        }

        public static string ToASCIIStringFromBase64(string base64String)
        {
            return ToASCIIStringFromBytes(Convert.FromBase64String(base64String));
        }

        public static string ToASCIIStringFromBytes(byte[] bytes)
        {
            return Encoding.ASCII.GetString(bytes);
        }

        public static string ToBase64FromASCIIString(string srcASCIIString)
        {
            return Convert.ToBase64String(ToBytesFromASCIIString(srcASCIIString));
        }

        public static string ToBase64FromBytes(byte[] bytes)
        {
            return Convert.ToBase64String(bytes);
        }

        public static string ToBase64FromDefaultEncoding(string srcUnicodeString)
        {
            return Convert.ToBase64String(ToBytesFromDefaultEncoding(srcUnicodeString));
        }

        public static string ToBase64FromGB2312(string srcGB2312String)
        {
            return Convert.ToBase64String(ToBytesFromGB2312(srcGB2312String));
        }

        public static string ToBase64FromUnicode(string srcUnicodeString)
        {
            return Convert.ToBase64String(ToBytesFromUnicode(srcUnicodeString));
        }

        /// <summary>
        /// 把ASCII字符串转换为byte[]数组
        /// </summary>
        /// <param name="ASCIIString"></param>
        /// <returns></returns>
        public static byte[] ToBytesFromASCIIString(string ASCIIString)
        {
            return Encoding.ASCII.GetBytes(ASCIIString);
        }

        /// <summary>
        /// 把Base64字符串转换为byte[]数组
        /// </summary>
        /// <param name="base64String"></param>
        /// <returns></returns>
        public static byte[] ToBytesFromBase64(string base64String)
        {
            return Convert.FromBase64String(base64String);
        }

        public static byte[] ToBytesFromDefaultEncoding(string srcDefaultEncodingString)
        {
            return Encoding.Default.GetBytes(srcDefaultEncodingString);
        }

        public static byte[] ToBytesFromGB2312(string srcGB2312String)
        {
            return GetGB2312Encoding().GetBytes(srcGB2312String);
        }

        public static byte[] ToBytesFromUnicode(string srcUnicodeString)
        {
            return Encoding.Unicode.GetBytes(srcUnicodeString);
        }

        public static byte[] ToBytesFromUTF8(string srcUnicodeString)
        {
            return Encoding.UTF8.GetBytes(srcUnicodeString);
        }

        public static string ToDefaultEncodingFromBase64(string base64String)
        {
            return ToDefaultEncodingFromBytes(Convert.FromBase64String(base64String));
        }

        public static string ToDefaultEncodingFromBytes(byte[] bytes)
        {
            return Encoding.Default.GetString(bytes);
        }

        public static string ToGB2312FromBytes(byte[] bytes)
        {
            return GetGB2312Encoding().GetString(bytes);
        }

        public static string ToGB2312StringFromBase64(string base64String)
        {
            return ToGB2312FromBytes(Convert.FromBase64String(base64String));
        }

        public static string ToUnicodeFromBase64(string base64String)
        {
            return ToUnicodeFromBytes(Convert.FromBase64String(base64String));
        }

        public static string ToUnicodeFromBytes(byte[] bytes)
        {
            return Encoding.Unicode.GetString(bytes);
        }

        public static string ToUTF8FromBytes(byte[] bytes)
        {
            return Encoding.UTF8.GetString(bytes);
        }
    }
}